import { http } from "@/utils/http";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175();
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43();
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

export const getAssetList = ({
  data,
  type = "asset",
}: {
  data: object;
  type: string;
}) => {
  return taskRequest<Result>("post", `/api/api/${type}/data`, data);
};

// ============ 威胁情报关联图谱 API ============

// 接口1: 获取图谱数据
export const getIntelligenceGraphData = (data?: {
  time_range?: string;
  severity_filter?: string[];
  source_filter?: string[];
  limit?: number;
  include_assets?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/graph/intelligence/data", data);
};

// 接口2: 获取筛选选项
export const getFilterOptions = (data?: object) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/intelligence/filter_options",
    data
  );
};

// 接口3: 获取节点详情
export const getNodeDetail = (data: { node_id: string; node_type: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/intelligence/node_detail",
    data
  );
};

// 接口4: 搜索节点
export const searchNodes = (data: {
  keyword: string;
  node_types?: string[];
  limit?: number;
}) => {
  return taskRequest<Result>("post", "/api/graph/intelligence/search", data);
};

// 接口5: 导出图谱
export const exportGraph = (data?: {
  format?: string;
  time_range?: string;
  severity_filter?: string[];
  source_filter?: string[];
  limit?: number;
  include_assets?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/graph/intelligence/export", data);
};

// 接口6: 获取统计数据
export const getStatistics = (data?: { time_range?: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/intelligence/statistics",
    data
  );
};

// ============ 资产风险图谱 API ============

// 接口1: 获取资产风险图谱数据
export const getAssetRiskGraphData = (data?: {
  department?: string;
  risk_levels?: string[];
  asset_types?: string[];
  severity_filter?: string[];
  limit?: number;
}) => {
  return taskRequest<Result>("post", "/api/graph/asset_risk/data", data);
};

// 接口2: 获取筛选选项
export const getAssetRiskFilterOptions = (data?: object) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/asset_risk/filter_options",
    data
  );
};

// 接口3: 获取资产详情
export const getAssetDetail = (data: { asset_ip: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/asset_risk/asset_detail",
    data
  );
};

// 接口4: 获取漏洞详情
export const getVulnerabilityDetail = (data: { cve_id: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/asset_risk/vulnerability_detail",
    data
  );
};

// 接口5: 生成风险报告
export const generateRiskReport = (data: {
  department?: string;
  report_type: string;
  format: string;
}) => {
  return taskRequest<Result>("post", "/api/graph/asset_risk/risk_report", data);
};

// 接口6: 获取风险统计
export const getAssetRiskStatistics = (data?: { department?: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/asset_risk/statistics",
    data
  );
};

// ============ 漏洞处置图谱 API ============

// 接口1: 获取漏洞处置图谱数据
export const getVulnDisposalGraphData = (data?: {
  time_range?: string;
  disposal_status?: string[];
  priority?: string[];
  vuln_types?: string[];
  responsible_person?: string;
  department?: string;
  limit?: number;
  include_tickets?: boolean;
  include_assets?: boolean;
  include_alerts?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/graph/vuln_disposal/data", data);
};

// 接口2: 获取筛选选项
export const getVulnDisposalFilterOptions = (data?: object) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/vuln_disposal/filter_options",
    data
  );
};

// 接口3: 获取节点详情
export const getVulnDisposalNodeDetail = (data: {
  node_id: string;
  node_type: string;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/vuln_disposal/node_detail",
    data
  );
};

// 接口4: 搜索节点
export const searchVulnDisposalNodes = (data: {
  keyword: string;
  node_types?: string[];
  limit?: number;
}) => {
  return taskRequest<Result>("post", "/api/graph/vuln_disposal/search", data);
};

// 接口5: 更新漏洞状态
export const updateVulnStatus = (data: {
  vuln_id: string;
  new_status: string;
  comment?: string;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/vuln_disposal/update_status",
    data
  );
};

// 接口6: 导出处置报告
export const exportDisposalReport = (data: {
  format?: string;
  filters?: {
    time_range?: string;
    disposal_status?: string[];
    priority?: string[];
  };
}) => {
  return taskRequest<Result>("post", "/api/graph/vuln_disposal/export", data);
};

// 接口7: 获取处置统计
export const getVulnDisposalStatistics = (data?: { time_range?: string }) => {
  return taskRequest<Result>(
    "post",
    "/api/graph/vuln_disposal/statistics",
    data
  );
};
